// packageUser/pages/transaction-list/index.js
import {getChanges, getDividends} from "../../../service/User";
import {accLess, numberToFixed} from "../../../utils/Util"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        active: 0,
        product: {
            list: [],
            loading: false,
            reset: false,
            error: false,
            total: 0
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({"product.loading": true})
    },

    /*
    * tabs 切换
    * */
    onTabsChange: function (e) {
        const that = this;
        clearTimeout(that._onTabsChange);
        that._onTabsChange = setTimeout(function () {
            let {index} = e.detail;
            let active = that.data.active;
            if (index !== active) {
                that.setData({
                    active: index,
                    "product.list": [],
                    "product.reset": true
                })
            }
        }, 300)
    },

    /*
    * 监听列表加载
    * */
    onLoadList: async function (e) {
        try {
            let paging = e.detail.paging;
            let active = this.data.active;
            let data = {
                "page": paging.page,
                "per-page": paging.pageSize
            };
            let list = this.data.product.list;
            let isReset = this.data.product.reset;
            let res = [], newList = [];
            if (active === 0) {
                res = await getChanges(data);
                newList = res.itemList.map(function (item) {
                    return {
                        c_fund_name: item.c_fund_name,
                        t_confirm_date: item.t_confirm_date,
                        c_busin_name: item.c_busin_name,
                        n_confirm_balance: item.n_confirm_balance,
                        n_confirm_shares: item.n_confirm_shares,
                        n_nav: item.n_nav
                    }
                })
            } else {
                res = await getDividends(data);
                newList = res.itemList.map(function (item) {
                    return {
                        c_fund_name: item.c_fund_name,
                        t_confirm_date: item.t_confirm_date,
                        n_real_balance: numberToFixed(accLess(item.n_total_profit, item.n_deduct_balance)),
                        c_dividend_type: item.c_dividend_type || "--"
                    }
                })
            }
            list = isReset ? newList : list.concat(newList);
            this.setData({
                "product.list": list,
                "product.total": res.totalCount
            });
        } catch (e) {
            this.setData({
                "product.error": true
            })
        }
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.setData({"product.loading": true})
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})